layui.use(['table', 'admin', 'ax','dtree','formSelects','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var dtree = layui.dtree;
    var fs = layui.formSelects;
    var form = layui.form;

    /**
     * 档案信息表管理
     */
    var ArchiveInfo = {
        tableId: "archiveInfoTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveInfo.initColumn = function () {
        return [[
            {field: 'wjid', width:100,sort: true, title: '卷宗号'},
            {field: 'mc',width:120, title: '档案名称'},
            {field: 'dalx',width:120,  title: '档案类型',templet:'#dalx'},
            // {field: 'ssbmid',width:200,  title: '所属部门'},
            {field: 'ssrrmc',width:120, title: '所属人'},
            {field: 'mj', width:120, title: '密级',templet:'#mj'},
            {field: 'glnx', width:120, title: '管理年限',templet:'#glnx'},
            {field: 'nd', width:120, title: '年度'},
            {field: 'qsnd', width:120,title: '起始年度'},
            {field: 'jsnd', width:120, title: '结束年度'},
            // {field: 'dahid', width:200,title: '档案盒'},
            {field: 'ly', width:120, title: '来源',templet:'#ly'},
            {field: 'sfyx', width:120,sort: true, title: '是否有效',templet:'#sfyx'},
            {field: 'bz',width:200, title: '备注'},
            {fixed:'right',width:120,align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };
    ArchiveInfo.queryData = {};
    /**
     * 点击查询按钮
     */
    ArchiveInfo.search = function () {
        var condi = $.extend({},ArchiveInfo.queryData,form.val('searchForm'));
        table.reload(ArchiveInfo.tableId, {
            where: condi, page: {curr: 1}
        });
    };

    //表单提交事件
    form.on('submit(btnSearch)', function (data) {
        ArchiveInfo.search();
        return false;
    });


    /**
     * 弹出添加对话框
     */
    ArchiveInfo.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveInfo/add';
    };

    /**
     * 弹出批量添加对话框
     */
    ArchiveInfo.openBatAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveInfo/batAdd';
    };

    /**
     * 导出excel按钮
     */
    ArchiveInfo.exportExcel = function () {
        var checkRows = table.checkStatus(ArchiveInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveInfo.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/archiveInfo/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveInfo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ArchiveInfo.tableId,
        url: Feng.ctxPath + '/archiveInfo/list',
        page: true,
        height: "full-100",
        cellMinWidth: 100,
        cols: ArchiveInfo.initColumn()
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ArchiveInfo.openAddDlg();
    });
    // 批量添加
    $('#btnBatAdd').click(function () {
        ArchiveInfo.openBatAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchiveInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchiveInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ArchiveInfo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ArchiveInfo.onDeleteItem(data);
        }
    });
    //左边树

    var urls = {
        "1":"archiveInfo/groupByNd",
        "2":"archiveInfo/groupByBm",
        "3":"archiveInfo/groupByDa",
    };

    var currentType = '1';
    var reload = function (type) {
        currentType = type;
        ArchiveInfo.queryData = {
            "nd":"",
            "ssbmid":"",
            "dalx":"",
            "dahid":""
        };
        dtree.reload('dTree', {
            url: Feng.ctxPath + urls[type],
            field:type
        });
        ArchiveInfo.search();
    };

    fs.render('type',{
        init:['1'],
        radio:true,
        on:function (id, vals, val, isAdd, isDisabled) {
            if (isAdd) {
                reload(val.value)
            }
            return true;
        }
    });

    dtree.render({
        elem: "#dTree",
        dataStyle: "layuiStyle",  //使用layui风格的数据格式
        skin: "layui",
        url: Feng.ctxPath + urls['1'],
        height:'full-100',
        width:'100%',
        initLevel:4,
        // response:{message:"message",statusCode:200}  //修改response中返回数据的定义
    });

    dtree.on("node('dTree')" ,function(obj){
        var data = obj.param;
        var level = data.level;
        var id = data.nodeId;
        ArchiveInfo.queryData = {
            "nd":"",
            "ssbmid":"",
            "dalx":"",
            "dahid":""
        };
        if (currentType == '1') {
            //年度，部门，类型
            var ids = id.split('-');
            ArchiveInfo.queryData['nd'] = ids[0]||'';
            ArchiveInfo.queryData['ssbmid'] = ids[1]||'';
            ArchiveInfo.queryData['dalx'] = ids[2]||'';
        }else if (currentType == '2') {
            //部门，年度，类型
            var ids = id.split('-');
            ArchiveInfo.queryData['ssbmid'] = ids[0]||'';
            ArchiveInfo.queryData['nd'] = ids[1]||'';
            ArchiveInfo.queryData['dalx'] = ids[2]||'';
        }else if (currentType == '3') {
            //档案盒id
            ArchiveInfo.queryData['dahid'] = id||'';
        }
        ArchiveInfo.search();
    });

});
